package top.canyue.eca_manager.mapper;

import org.apache.ibatis.annotations.*;
import top.canyue.eca_manager.bean.ConferenceDetail;
import top.canyue.eca_manager.bean.ReDetail;
import top.canyue.eca_manager.provider.ConferenceDetailProvider;

import java.util.List;
import java.util.Map;

@Mapper
public interface ConferenceDetailMapper {
    //持久化缓存
    @UpdateProvider(type = ConferenceDetailProvider.class, method = "persistentCache")
    Integer persistentCache(String conferenceId, String userId, Map<String, Integer> data);

    @Select(value = "SELECT * FROM conferencedetail WHERE id = #{id}")
    ConferenceDetail getDetailById(String id);

    @Insert(value = "INSERT INTO conferencedetail(id,conference_id,user_id,createTime) VALUES(#{uuid},#{conferenceId},#{userId},NOW())")
    Integer joinConference(String uuid,String conferenceId,String userId);

    @Update(value = "UPDATE conferencedetail SET isDelete = 1, id = MD5(UUID()) WHERE user_id = #{userId} AND conference_id = #{conferenceId}")
    Integer leaveConference(String userId, String conferenceId);

    @UpdateProvider(type = ConferenceDetailProvider.class, method = "leaveConferenceByAllUser")
    Integer leaveConferenceByAllUser(List<String> userIdList, String conferenceId);

    @Select(value = "SELECT DISTINCT user_id FROM conferencedetail WHERE conference_id = #{conferenceId} AND NOT isDelete")
    List<String> getUserIdByConferenceId(String conferenceId);

    @Select(value = "SELECT DISTINCT u.id, u.name " +
            "FROM conferencedetail AS d JOIN user AS u ON d.user_id = u.id " +
            "WHERE d.conference_id = #{conferenceId} AND NOT d.isDelete AND NOT u.isDelete ")
    List<Map<String, Object>> getUserNameByConferenceId(String conferenceId);

    @Select(value = "SELECT cd.id,cd.user_id,u.name,u.phone,u.admin,cd.sad,cd.angry,cd.surprise,cd.fear,cd.happy,cd.disgust,cd.neutral " +
            "FROM (SELECT * FROM conferencedetail WHERE conference_id = #{conferenceId} AND isDelete = 0) AS cd " +
            "INNER JOIN (SELECT * FROM `user` WHERE isDelete = 0) AS u " +
            "ON u.id = cd.user_id ")
    List<ReDetail> getDetailByConferenceId(String conferenceId);
}
